package com.yuesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuesystem.common.BaseContext;
import com.yuesystem.common.CustomException;
import com.yuesystem.dto.BlogFront;
import com.yuesystem.dto.responsebody.BlogCheckResponseBody;
import com.yuesystem.dto.responsebody.BlogDetailResponseBody;
import com.yuesystem.dto.responsebody.BlogResponseBody;
import com.yuesystem.dto.responsebody.CollectBlogResponseBody;
import com.yuesystem.dto.responsebody.CommentResponseBody;
import com.yuesystem.dto.responsebody.LikeResponseBody;
import com.yuesystem.dto.responsebody.ReplyResponseBody;
import com.yuesystem.entity.Blog;
import com.yuesystem.entity.CollectBlog;
import com.yuesystem.entity.CollectItem;
import com.yuesystem.entity.Comment;
import com.yuesystem.entity.Like;
import com.yuesystem.entity.Reply;
import com.yuesystem.entity.User;
import com.yuesystem.mapper.BlogMapper;
import com.yuesystem.service.BlogService;
import com.yuesystem.service.CollectBlogService;
import com.yuesystem.service.CollectItemService;
import com.yuesystem.service.CommentService;
import com.yuesystem.service.LikeService;
import com.yuesystem.service.ReplyService;
import com.yuesystem.service.UserService;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;



/**
 * 帖子接口实现类.
 */
@Service
@Slf4j
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {
  @Autowired
  private UserService userService;
  @Autowired
  private CommentService commentService;
  @Autowired
  private LikeService likeService;
  @Autowired
  private CollectBlogService collectBlogService;
  @Autowired
  private ReplyService replyService;
  @Autowired
  private CollectItemService collectItemService;
  
  /**
   * 发布帖子.
   *
   * @param blog 帖子类
   */
  @Override
  public void saveBlog(@RequestBody Blog blog) {
    if (blog.getContent().isEmpty()) {
      throw new CustomException("帖子内容不能为空，请编辑帖子内容后重试！");
    }
    if (blog.getTitle().isEmpty()) {
      throw new CustomException("帖子标题不能为空，请编辑帖子标题后重试！");
    }
    // 获取发布者id
    Long userId = BaseContext.getCurrentId();
    blog.setUserId(userId);
    
    this.save(blog);
  }
  
  /**
   * 根据分类返回下标.
   */
  @Override
  public int help1(String type0) {
    int flag0 = 100;
    if (type0.equals("旅游攻略")) {
      flag0 = 0;
    }
    if (type0.equals("新闻资讯")) {
      flag0 = 1;
    }
    if (type0.equals("书院趣事")) {
      flag0 = 2;
    }
    if (type0.equals("官职相关")) {
      flag0 = 3;
    }
    return flag0;
  }
  
  /**
   * 根据全部类别返回列表.
   */
  @Override
  public List<Integer> help2(String type0, String type1, String type2, String type3) {
    int flag0 = this.help1(type0);
    int flag1 = this.help1(type1);
    int flag2 = this.help1(type2);
    int flag3 = this.help1(type3);
    List<Integer> list = new ArrayList<>();
    list.add(flag0);
    list.add(flag1);
    list.add(flag2);
    list.add(flag3);
    return list;
  }
  
  /**
   * 根据下标返回类型.
   */
  @Override
  public String help3(String type) {
    String theme = "";
    if (type.equals("0")) {
      theme = ("旅游攻略");
    }
    if (type.equals("1")) {
      theme = ("新闻资讯");
    }
    if (type.equals("2")) {
      theme = ("书院趣事");
    }
    if (type.equals("3")) {
      theme = ("官职相关");
    }
    return theme;
  }
  
  /**
   * 分页查询帖子(首页).
   *
   * @param blogFront 前台帖子类
   * @return 帖子响应类
   */
  @Override
  public Page<BlogResponseBody> getPage(BlogFront blogFront) {
    String type0 = blogFront.getType0();
    String type1 = blogFront.getType1();
    String type2 = blogFront.getType2();
    String type3 = blogFront.getType3();
    List<Integer> list = this.help2(type0, type1, type2, type3);
    LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.in(Blog::getType, list);
    queryWrapper.orderByDesc(Blog::getCreateTime);
    queryWrapper.eq(Blog::getStatus, 1);
    int page = blogFront.getPage();
    int pageSize = blogFront.getPageSize();
    Page<Blog> blogPage = new Page<>(page, pageSize);
    Page<BlogResponseBody> dtoPage = new Page<>(page, pageSize);
    this.page(blogPage, queryWrapper);
    BeanUtils.copyProperties(blogPage, dtoPage, "records");
    List<Blog> records = blogPage.getRecords();
    List<BlogResponseBody> dtoList = records.stream().map((item) -> {
      BlogResponseBody blogResponseBody = new BlogResponseBody();
      BeanUtils.copyProperties(item, blogResponseBody);
      Long userId = item.getUserId();
      User one = userService.getById(userId);
      String userName = one.getUserName();
      String type = String.valueOf(item.getType());
      String theme = this.help3(type);
      Long blogId = item.getId();
      Long likeCounts = likeService.getLikeCount(blogId);
      Long commentCounts = commentService.getCommentCount(blogId);
      Long collectCounts = collectBlogService.getCollectCount(blogId);
      if (one != null) {
        blogResponseBody.setTheme(theme);
        blogResponseBody.setAuthor(userName);
        blogResponseBody.setCommentCounts(commentCounts);
        blogResponseBody.setCollectCounts(collectCounts);
        blogResponseBody.setLikeCounts(likeCounts);
      }
      return blogResponseBody;
    }).collect(Collectors.toList());
    dtoPage.setRecords(dtoList);
    return dtoPage;
  }
  
  /**
   * 查询单条帖子（首页）.
   *
   * @param blogId 帖子id
   * @return 帖子响应类
   */
  @Override
  public BlogResponseBody blogOne(Long blogId) {
    LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Blog::getId, blogId);
    Blog one = this.getOne(queryWrapper);
    if (one == null) {
      throw new CustomException("该帖子不存在！");
    }
    BlogResponseBody blogResponseBody = new BlogResponseBody();
    BeanUtils.copyProperties(one, blogResponseBody);
    // 获得帖子类型id,换成类型名称
    String type = String.valueOf(one.getType());
    String theme = this.help3(type);
    // 获得帖子标题
    String title = one.getTitle();
    // 获得帖子点赞量
    Long likeCounts = likeService.getLikeCount(blogId);
    // 获得帖子回复量
    Long commentCounts = commentService.getCommentCount(blogId);
    // 获得帖子收藏量
    Long collectCounts = collectBlogService.getCollectCount(blogId);
    blogResponseBody.setTitle(title);
    blogResponseBody.setLikeCounts(likeCounts);
    blogResponseBody.setCommentCounts(commentCounts);
    blogResponseBody.setCollectCounts(collectCounts);
    blogResponseBody.setTheme(theme);
    // 获取发布者用户名
    Long userId = one.getUserId();
    User user = userService.getById(userId);
    String author = user.getUserName();
    blogResponseBody.setAuthor(author);
    // 获得发布者头像地址
    String img = user.getImg();
    blogResponseBody.setImg(img);
    return blogResponseBody;
  }
  
  /**
   * 根据选择返回我的发布的queryWrapper.
   */
  @Override
  public LambdaQueryWrapper<Blog> help4(Long userId, String startTime, String endTime) {
    LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Blog::getUserId, userId);
    if ((!startTime.isEmpty()) && (!endTime.isEmpty())) {
      // 将String类型的时间转化成为LocalDateTime类型的时间
      startTime = startTime + " 00:00:00";
      endTime = endTime + " 23:59:59";
      
      DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
      LocalDateTime start = LocalDateTime.parse(startTime, df);
      LocalDateTime end = LocalDateTime.parse(endTime, df);
      
      queryWrapper.ge(Blog::getCreateTime, start)
          .le(Blog::getCreateTime, end);
      
    } else if (!startTime.isEmpty()) {
      startTime = startTime + " 00:00:00";
      DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
      LocalDateTime start = LocalDateTime.parse(startTime, df);
      
      queryWrapper.ge(Blog::getCreateTime, start);
      
    } else if (!endTime.isEmpty()) {
      endTime = endTime + " 23:59:59";
      DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
      LocalDateTime end = LocalDateTime.parse(endTime, df);
      
      queryWrapper.le(Blog::getCreateTime, end);
    }
    return queryWrapper;
  }
  
  /**
   * 分页查看（我的发布）.
   *
   * @param page      起始页
   * @param pageSize  页数
   * @param startTime 起始时间
   * @param endTime   结束时间
   * @return 帖子响应类分页
   */
  @Override
  public Page<BlogResponseBody> getPageMyBlog(
      int page, int pageSize, String startTime, String endTime) {
    // 获取当前用户id
    Long userId = BaseContext.getCurrentId();
    LambdaQueryWrapper<Blog> queryWrapper = this.help4(userId, startTime, endTime);
    Page<Blog> pageInfo = new Page<>(page, pageSize);
    queryWrapper.orderByDesc(Blog::getCreateTime);
    this.page(pageInfo, queryWrapper);
    Page<BlogResponseBody> dtoPage = new Page<>(page, pageSize);
    BeanUtils.copyProperties(pageInfo, dtoPage, "records");
    List<Blog> records = pageInfo.getRecords();
    List<BlogResponseBody> dtoList = records.stream().map((item) -> {
      BlogResponseBody blogResponseBody = new BlogResponseBody();
      BeanUtils.copyProperties(item, blogResponseBody);
      Long blogId = item.getId();
      Long likeCounts = likeService.getLikeCount(blogId);
      Long commentCounts = commentService.getCommentCount(blogId);
      Long collectCounts = collectBlogService.getCollectCount(blogId);
      blogResponseBody.setLikeCounts(likeCounts);
      blogResponseBody.setCollectCounts(collectCounts);
      blogResponseBody.setCommentCounts(commentCounts);
      return blogResponseBody;
    }).collect(Collectors.toList());
    dtoPage.setRecords(dtoList);
    return dtoPage;
  }
  
  /**
   * 根据选择返回我的收藏的queryWrapper.
   */
  @Override
  public LambdaQueryWrapper<CollectBlog> help5(Long userId, String startTime, String endTime) {
    LambdaQueryWrapper<CollectBlog> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(CollectBlog::getUserId, userId);
    if ((!startTime.isEmpty()) && (!endTime.isEmpty())) {
      // 将String类型的时间转化成为LocalDateTime类型的时间
      startTime = startTime + " 00:00:00";
      endTime = endTime + " 23:59:59";
      DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
      LocalDateTime start = LocalDateTime.parse(startTime, df);
      LocalDateTime end = LocalDateTime.parse(endTime, df);
      queryWrapper.ge(CollectBlog::getCreateTime, start).le(CollectBlog::getCreateTime, end);
    } else if (!startTime.isEmpty()) {
      startTime = startTime + " 00:00:00";
      DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
      LocalDateTime start = LocalDateTime.parse(startTime, df);
      queryWrapper.ge(CollectBlog::getCreateTime, start);
    } else if (!endTime.isEmpty()) {
      endTime = endTime + " 23:59:59";
      DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
      LocalDateTime end = LocalDateTime.parse(endTime, df);
      queryWrapper.le(CollectBlog::getCreateTime, end);
    }
    return queryWrapper;
  }
  
  /**
   * 分页查看我的收藏.
   *
   * @param page      起始页
   * @param pageSize  页数
   * @param startTime 起始时间
   * @param endTime   结束时间
   * @return 收藏帖子响应类分页
   */
  @Override
  public Page<CollectBlogResponseBody> getPageMyCollect(
      int page, int pageSize, String startTime, String endTime) {
    Long userId = BaseContext.getCurrentId();
    LambdaQueryWrapper<CollectBlog> queryWrapper = this.help5(userId, startTime, endTime);
    Page<CollectBlog> pageInfo = new Page<>(page, pageSize);
    queryWrapper.orderByDesc(CollectBlog::getCreateTime);
    collectBlogService.page(pageInfo, queryWrapper);
    Page<CollectBlogResponseBody> dtoPage = new Page<>(page, pageSize);
    BeanUtils.copyProperties(pageInfo, dtoPage, "records");
    List<CollectBlog> records = pageInfo.getRecords();
    List<CollectBlogResponseBody> dtoList = records.stream().map((item) -> {
      CollectBlogResponseBody collectBlogResponseBody = new CollectBlogResponseBody();
      BeanUtils.copyProperties(item, collectBlogResponseBody);
      Long blogId = item.getBlogId();
      Blog blog = this.getById(blogId);
      Long oneUserId = blog.getUserId();
      User one = userService.getById(oneUserId);
      String oneName = one.getUserName();
      String type = String.valueOf(blog.getType());
      String theme = this.help3(type);
      String title = blog.getTitle();
      Long likeCounts = likeService.getLikeCount(blogId);
      Long commentCounts = commentService.getCommentCount(blogId);
      Long collectCounts = collectBlogService.getCollectCount(blogId);
      if (blog != null) {
        collectBlogResponseBody.setCollectCounts(collectCounts);
        collectBlogResponseBody.setLikeCounts(likeCounts);
        collectBlogResponseBody.setCommentCounts(commentCounts);
        collectBlogResponseBody.setTheme(theme);
        collectBlogResponseBody.setTitle(title);
        collectBlogResponseBody.setAuthorName(oneName);
      }
      return collectBlogResponseBody;
    }).collect(Collectors.toList());
    dtoPage.setRecords(dtoList);
    return dtoPage;
  }
  
  
  /**
   * 分页查看别人对我的帖子的收藏.
   *
   * @param page     起始页
   * @param pageSize 页数
   * @return 收藏帖子响应类分页
   */
  @Override
  public Page<CollectBlogResponseBody> getPageCollectMy(int page, int pageSize) {
    Long currentId = BaseContext.getCurrentId();
    LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Blog::getUserId, currentId);
//    获取该用户发布的所有blog
    List<Blog> blogList = this.list(queryWrapper);
    Page<CollectBlogResponseBody> dtoPage = new Page<>(page, pageSize);
    if (blogList.size() == 0 || blogList == null) {
      return dtoPage;
    }
    //    获取该用户发布的所有blogId
    List<Long> myBlogIdList = new ArrayList<>();
    for (Blog s : blogList) {
      myBlogIdList.add(s.getId());
    }
    LambdaQueryWrapper<CollectItem> queryWrapper1 = new LambdaQueryWrapper<>();
    queryWrapper1.in(CollectItem::getType, "blog");
    queryWrapper1.in(CollectItem::getCollectId, myBlogIdList);
    Page<CollectItem> pageInfo = new Page<>(page, pageSize);
    collectItemService.page(pageInfo, queryWrapper1);
    BeanUtils.copyProperties(pageInfo, dtoPage, "records");
    List<CollectItem> records = pageInfo.getRecords();
    List<CollectBlogResponseBody> dtoList = records.stream().map((item) -> {
      CollectBlogResponseBody collectBlogResponseBody = new CollectBlogResponseBody();
      Long userId = item.getUserId();
      BeanUtils.copyProperties(item, collectBlogResponseBody);
      User user = userService.getById(userId);
      String userName = user.getUserName();
      String img = user.getImg();
      Long blogId = item.getCollectId();
      Blog blog = this.getById(blogId);
      String title = blog.getTitle();
      if (item != null) {
        collectBlogResponseBody.setUserName(userName);
        collectBlogResponseBody.setImg(img);
        collectBlogResponseBody.setTitle(title);
        collectBlogResponseBody.setBlogId(blogId);
      }
      return collectBlogResponseBody;
    }).collect(Collectors.toList());
    dtoPage.setRecords(dtoList);
    return dtoPage;
  }
  
  /**
   * 分页查看别人对我的帖子的点赞.
   *
   * @param page     起始页
   * @param pageSize 页数
   * @return 点赞响应类分页
   */
  @Override
  public Page<LikeResponseBody> getPageLikeMy(int page, int pageSize) {
    Long currentId = BaseContext.getCurrentId();
    LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Blog::getUserId, currentId);
    List<Blog> blogList = this.list(queryWrapper);
    Page<LikeResponseBody> dtoPage = new Page<>(page, pageSize);
    if (blogList.size() == 0 || blogList == null) {
      return dtoPage;
    }
    List<Long> myBlogIdList = new ArrayList<>();
    for (Blog s : blogList) {
      myBlogIdList.add(s.getId());
    }
    LambdaQueryWrapper<Like> queryWrapper1 = new LambdaQueryWrapper<>();
    queryWrapper1.in(Like::getBlogId, myBlogIdList);
    queryWrapper1.orderByDesc(Like::getCreateTime);
    Page<Like> pageInfo = new Page<>(page, pageSize);
    likeService.page(pageInfo, queryWrapper1);
    BeanUtils.copyProperties(pageInfo, dtoPage, "records");
    List<Like> records = pageInfo.getRecords();
    List<LikeResponseBody> dtoList = records.stream().map((item) -> {
      Long userId = item.getUserId();
      LikeResponseBody likeResponseBody = new LikeResponseBody();
      BeanUtils.copyProperties(item, likeResponseBody);
      User user = userService.getById(userId);
      String userName = user.getUserName();
      String img = user.getImg();
      Long blogId = item.getBlogId();
      Blog blog = this.getById(blogId);
      String title = blog.getTitle();
      if (item != null) {
        likeResponseBody.setUserName(userName);
        likeResponseBody.setImg(img);
        likeResponseBody.setTitle(title);
      }
      return likeResponseBody;
    }).collect(Collectors.toList());
    dtoPage.setRecords(dtoList);
    return dtoPage;
  }
  
  /**
   * 分页查看别人对我的帖子的评论.
   *
   * @param page     起始页
   * @param pageSize 页数
   * @return 评论响应类分页
   */
  @Override
  public Page<CommentResponseBody> getPageCommentMy(int page, int pageSize) {
    Long currentId = BaseContext.getCurrentId();
    LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Blog::getUserId, currentId);
    List<Blog> blogList = this.list(queryWrapper);
    Page<CommentResponseBody> dtoPage = new Page<>(page, pageSize);
    if (blogList.size() == 0 || blogList == null) {
      return dtoPage;
    }
    List<Long> myBlogIdList = new ArrayList<>();
    for (Blog s : blogList) {
      myBlogIdList.add(s.getId());
    }
    LambdaQueryWrapper<Comment> queryWrapper1 = new LambdaQueryWrapper<>();
    queryWrapper1.in(Comment::getBlogId, myBlogIdList);
    queryWrapper1.orderByDesc(Comment::getCreateTime);
    Page<Comment> pageInfo = new Page<>(page, pageSize);
    commentService.page(pageInfo, queryWrapper1);
    BeanUtils.copyProperties(pageInfo, dtoPage, "records");
    List<Comment> records = pageInfo.getRecords();
    List<CommentResponseBody> dtoList = records.stream().map((item) -> {
      Long userId = item.getUserId();
      CommentResponseBody commentResponseBody = new CommentResponseBody();
      BeanUtils.copyProperties(item, commentResponseBody);
      User user = userService.getById(userId);
      String userName = user.getUserName();
      String img = user.getImg();
      Long blogId = item.getBlogId();
      Blog blog = this.getById(blogId);
      String title = blog.getTitle();
      if (item != null) {
        commentResponseBody.setUserName(userName);
        commentResponseBody.setImg(img);
        commentResponseBody.setTitle(title);
      }
      return commentResponseBody;
    }).collect(Collectors.toList());
    dtoPage.setRecords(dtoList);
    return dtoPage;
  }
  
  /**
   * 根据用户id删除该用户所有的发帖.
   *
   * @param userId 用户id
   */
  @Override
  @Transactional
  public void deleteById(Long userId) {
    likeService.deleteById(userId);
    collectBlogService.deleteById(userId);
    LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Blog::getUserId, userId);
    List<Blog> list = this.list(queryWrapper);
    if (list.size() == 0) {
      return;
    }
    List<Long> listBlog = list.stream().map((item) -> {
      return item.getId();
    }).collect(Collectors.toList());
    
    // 删除发帖前先删除对该用户发帖的所有点赞，收藏，评论，回复
    likeService.deleteByBlog(listBlog);
    
    collectBlogService.deleteByBlog(listBlog);
    
    replyService.deleteByBlog(listBlog);
    
    commentService.deleteByBlog(listBlog);
    
    // 删除发帖
    LambdaQueryWrapper<Blog> queryWrapper1 = new LambdaQueryWrapper<>();
    queryWrapper1.in(listBlog.size() != 0, Blog::getId, listBlog);
    this.remove(queryWrapper1);
  }
  
  /**
   * 根据帖子id,返回所有的以及一级评论和二级评论.
   *
   * @param blogId 帖子id
   * @return 评论响应类分页
   */
  @Override
  public List<CommentResponseBody> getComment(Long blogId) {
    LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Comment::getBlogId, blogId);
    List<Comment> commentsList = commentService.list(queryWrapper);
    List<CommentResponseBody> commentResponseBodyList = commentsList.stream().map((item) -> {
      CommentResponseBody commentResponseBody = new CommentResponseBody();
      BeanUtils.copyProperties(item, commentResponseBody);
      Long userId = item.getUserId();
      User one = userService.getById(userId);
      String userName = one.getUserName();
      String commentImg = one.getImg();
      if (one != null) {
        commentResponseBody.setUserName(userName);
        commentResponseBody.setImg(commentImg);
      }
      Long parentCommentId = item.getId();
      LambdaQueryWrapper<Reply> queryWrapper1 = new LambdaQueryWrapper<>();
      queryWrapper1.eq(Reply::getParentCommentId, parentCommentId);
      List<Reply> replyList = replyService.list(queryWrapper1);
      List<ReplyResponseBody> replyResponseBodyList = replyList.stream().map((item1) -> {
        ReplyResponseBody replyResponseBody = new ReplyResponseBody();
        BeanUtils.copyProperties(item1, replyResponseBody);
        Long userIdNow = item1.getUserId();
        User oneNow = userService.getById(userIdNow);
        String userNameNow = oneNow.getUserName();
        String replyImg = oneNow.getImg();
        if (oneNow != null) {
          replyResponseBody.setUserName(userNameNow);
          replyResponseBody.setCommentUserName(userName);
          replyResponseBody.setImg(replyImg);
        }
        return replyResponseBody;
      }).collect(Collectors.toList());
      commentResponseBody.setReplies(replyResponseBodyList);
      return commentResponseBody;
    }).collect(Collectors.toList());
    return commentResponseBodyList;
  }
  
  /**
   * 获取未审核帖子列表.
   *
   * @param page     起始页
   * @param pageSize 页数
   * @return 审核帖子响应类分页
   */
  @Override
  public Page<BlogCheckResponseBody> getCheckPage(int page, int pageSize) {
    
    Page<Blog> pageInfo = new Page<>(page, pageSize);
    Page<BlogCheckResponseBody> dtoPage = new Page<>(page, pageSize);
    LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Blog::getStatus, 0)
        .orderByDesc(Blog::getCreateTime);
    this.page(pageInfo, queryWrapper);
    
    BeanUtils.copyProperties(pageInfo, dtoPage, "records");
    
    List<Blog> records = pageInfo.getRecords();
    
    List<BlogCheckResponseBody> list = records.stream().map((blog) -> {
      BlogCheckResponseBody body = new BlogCheckResponseBody();
      BeanUtils.copyProperties(blog, body);
      body.setId(blog.getId());
      String author = userService.getById(blog.getUserId()).getUserName();
      body.setAuthor(author);
      body.setTitle(blog.getTitle());
      body.setCreateTime(blog.getCreateTime());
      body.setStatus("未审核");
      return body;
    }).collect(Collectors.toList());
    
    dtoPage.setRecords(list);
    
    return dtoPage;
  }
  
  /**
   * 获取审核界面的帖子详细内容.
   *
   * @param id 帖子id
   * @return 审核帖子详细内容响应类分页
   */
  @Override
  public BlogDetailResponseBody getBlogDetail(int id) {
    Blog blog = this.getById(id);
    BlogDetailResponseBody body = new BlogDetailResponseBody();
    body.setId(blog.getId());
    body.setTitle(blog.getTitle());
    body.setContent(blog.getContent());
    body.setCreateTime(blog.getCreateTime());
    String author = userService.getById(blog.getUserId()).getUserName();
    body.setAuthor(author);
    String img = userService.getById(blog.getUserId()).getImg();
    body.setImg(img);
    return body;
  }
  
  /**
   * 允许发布.
   *
   * @param id 帖子id
   */
  @Override
  public void allowed(int id) {
    Blog blog = this.getById(id);
    blog.setStatus(1);
    this.updateById(blog);
  }
  
  /**
   * 禁止发布.
   *
   * @param id 帖子id
   */
  @Override
  public void refused(int id) {
    Blog blog = this.getById(id);
    blog.setStatus(2);
    this.updateById(blog);
  }
}
